package puzzle.projecteuler.p200;

public class Problem138 {

	/**
	 *    L^2 = h^2 + b^2/4
	 *    
	 * => b = 2x, L^2 = (2x +/- 1)^2 + x^2
	 * => 5*L^2 - (5x +/- 2)^2 = 1
	 * 
	 * so, for equation: 5A^2 - B^2 =1
	 * A*sqrt(5) + B = (sqrt(5) + 2)^n
	 * 
	 * A_n = 2*A_(n-1) + B_(n-1)
	 * B_n = 5*A_(n-1) + 2*B_(n-1)
	 * @param args
	 */
	public static void main(String[] args) {
		
		long a = 1;
		long b = 2;
		int c = 1;
		long L = 0;
		while (c < 13) {
			long x = a;
			a = 2*a + b;
			b = 5*x + 2*b;
			
			if (b%5 == 2 || b%5 == 3) {
				L += a;
				c ++;
			}
		}
		System.out.println(L);
	}
}
